package com.xy.bioapi.utils;

import com.xy.bioapi.enums.ResultEnum;
import com.xy.bioapi.exception.TestException;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求签名
 * Created by Caeyer on 2017/7/13.
 */
public class SignUtil {

    private final static Logger logger = LoggerFactory.getLogger(SignUtil.class);

    /**
     * 签名比较方法
     * @param request
     */
    public void compareSign(HttpServletRequest request){
        String key = request.getParameter("key");
        String sign = request.getParameter("sign");
        if(key != null && !"".equals(key) && "test".equals(key)){
            StringBuffer sb = new StringBuffer();
            sb.append("key=");
            sb.append(request.getParameter("key"));
            sb.append("&code=");
            sb.append(request.getParameter("code"));
            sb.append("&v=");
            sb.append(request.getParameter("v")!=null?request.getParameter("v"):"1.0");
            sb.append("&format=");
            sb.append(request.getParameter("format")!=null?request.getParameter("format"):"json");
            sb.append("&method=");
            sb.append(request.getParameter("mehtod"));
            logger.info("sb = {}",sb.toString());
            logger.info("sign = {}",DigestUtils.md5Hex(sb.toString()));
//            if(!DigestUtils.md5Hex(sb.toString()).equals(sign)){
//                throw new TestException(ResultEnum.SIGNATURE_ERROR);
//            }
        }else{
            throw new TestException(ResultEnum.INVAILD_KEY);
        }
    }
}
